package com.java.zhuiyun.api.SysUser.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.java.zhuiyun.api.SysUser.controller.dto.UserPasswordDTO;
import com.java.zhuiyun.api.SysUser.entity.Password;
import com.java.zhuiyun.api.SysUser.entity.SysUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @InterfaceName SysUserMapper
 * @Description TODO
 * @Author xy
 * @Date 2023/8/14 19:51
 **/
@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {

    /**
    * @Author xy
    * @Description 全查
    * @Date 2023/8/15 18:49
    * @Return  * @param null
    */
    @Select("<script>select * from sys_user where del_flag = 0 <when test='userName != null and userName != &quot;&quot;'> " +
            "and user_name like concat('%', #{userName}, '%') </when></script>")
    List<SysUser> userList(SysUser sysUser);

    /**
    * @Author xy
    * @Description 添加
    * @Date 2023/8/15 20:10
    * @Return  * @param null
    */
    @Insert("insert into sys_user (user_name,password,nick_name,gender,email,phone,address,avatar_url,role,create_time,update_time) values " +
            "(#{userName},#{password},#{nickName},#{gender},#{email},#{phone},#{address},#{avatarUrl},#{role},#{createTime},#{updateTime})")
    int SysUserAdd(SysUser sysUser);

    /**
     * @Author xy
     * @Description 逻辑删除
     * @Date 2023/8/15 16:41
     * @Return  * @param null
     */
    @Update("update sys_user set del_flag = 1 where id = #{id}")
    int SysUserDelete(Integer id);

    @Update("update sys_user set user_name = #{userName},nick_name = #{nickName},gender = #{gender},email = #{email}," +
            "phone = #{phone},address = #{address},avatar_url = #{avatarUrl},role = #{role},update_time = #{updateTime} where id = #{id}")
    int SysUserupdate(SysUser sysUser);

    @Update("update sys_user set is_disable = #{isDisable} where id = #{id}")
    int updatescope(SysUser sysUser);

    @Update("update sys_user set password = #{newPassword} where user_name = #{userName} and password = #{password}")
    int updatePassword(UserPasswordDTO userPasswordDTO);

    @Select("select * from sys_user where user_name = #{userName}")
    SysUser selectList(@Param("userName") String userName);

    @Update("<script>update sys_user set " +
            "<if test='nickName!=null and nickName!=&quot;&quot;'>nick_name=#{nickName},</if>" +
            "<if test='phone!=null and phone!=&quot;&quot;'>phone=#{phone},</if>" +
            "<if test='email!=null and email!=&quot;&quot;'>email=#{email},</if>" +
            "<if test='address!=null and address!=&quot;&quot;'>address=#{address},</if>" +
            "<if test='gender!=null and gender!=&quot;&quot;'>gender=#{gender},</if>" +
            "<if test='avatarUrl!=null and avatarUrl!=&quot;&quot;'>avatar_url=#{avatarUrl},</if>" +
            "<if test='updateTime!=null'>update_time=#{updateTime} </if>" +
            "where id=#{id}" +
            "</script>")
    Integer updateInfo(SysUser sysUser);

    //根据id查询用户信息
    @Select("select id, user_name, nick_name, gender, email, phone, address, avatar_url, role, password from sys_user where id=#{id}")
    SysUser selectById(Integer id);

    @Update("update sys_user set password=#{newPassword} where id=#{id}")
    Integer savePassword(Password password);

    @Insert("insert into sys_user (user_name,nick_name,password,gender,email,phone,address,avatar_url,role,create_time,update_time) values " +
            "(#{userName},#{nickName},#{password},#{gender},#{email},#{phone},#{address},#{avatarUrl},#{role},#{createTime},#{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insertInfo(SysUser sysUser);
}
